

if ( SERVER ) then

	AddCSLuaFile( "shared.lua" )
	
end

if ( CLIENT ) then

	SWEP.PrintName			= "USP"			
	SWEP.Author				= "Counter-Strike"
	SWEP.Slot				= 1
	SWEP.SlotPos			= 0
	SWEP.IconLetter			= "a"
	
	killicon.AddFont( "weapon_usp", "CSKillIcons", SWEP.IconLetter, Color( 255, 80, 0, 255 ) )
	
end

SWEP.HoldType			= "pistol"
SWEP.Base				= "weapon_cs_base2_ds"

SWEP.Spawnable			= true
SWEP.AdminSpawnable		= true

SWEP.ViewModel 			= "models/weapons/v_pist_usp.mdl"
SWEP.WorldModel 		= "models/weapons/w_pist_usp.mdl"

SWEP.Weight				= 5
SWEP.AutoSwitchTo		= false
SWEP.AutoSwitchFrom		= false
SWEP.AutoReloading		= false
SWEP.NextReload			= 0

SWEP.Primary.Sound 			= Sound( "Weapon_USP.Single" )
SWEP.Primary.Damage 		= 10
SWEP.Primary.Recoil 		= 1
SWEP.Primary.NumShots 		= 1
SWEP.Primary.Cone 			= 0.0155
SWEP.Primary.ClipSize 		= 12
SWEP.Primary.Delay 			= 0.12
SWEP.Primary.DefaultClip 	= 112
SWEP.Primary.Automatic 		= false
SWEP.Primary.Ammo 			= "pistol"


SWEP.Secondary.ClipSize		= -1
SWEP.Secondary.DefaultClip	= -1
SWEP.Secondary.Automatic	= false
SWEP.Secondary.Ammo			= "none"
SWEP.Secondary.Delay 		= 3

function SWEP:PrimaryAttack()

	self.Weapon:SetNextPrimaryFire( CurTime() + self.Primary.Delay )
	self.Weapon:SetNextSecondaryFire( CurTime() + self.Primary.Delay )

	if !self:CanPrimaryAttack() or self.Owner:WaterLevel() > 2 or self.Weapon:Clip1() < 1 then return end
	// Play shoot sound
	if self.Weapon:GetNetworkedBool( "Silenced", false ) then
		self.Primary.Sound = Sound("Weapon_USP.SilencedShot")
		self.CSMuzzleFlashes = false
	else
		self.Primary.Sound = Sound("Weapon_USP.Single")
		self.CSMuzzleFlashes = true
	end

	self.Weapon:EmitSound( self.Primary.Sound )
	
	// Shoot the bullet
	local LastShootTime = self.Weapon:GetNetworkedFloat( "LastShootTime", 0 )
	local Crouch = ( self.Owner:Crouching() and self.Owner:OnGround() ) and 2 or 1

	self:CSShootBullet( self.Primary.Damage, self.Primary.Recoil, self.Primary.NumShots, self.Primary.Cone / math.Clamp( ( CurTime() - LastShootTime ) * 1.3 , 0.001, 1 ) / Crouch )
	
	if self.Owner:IsNPC() then return end

	// Punch the player's view
	self.Owner:ViewPunch( Angle( math.Rand( -0.5, -0.1 ) * self.Primary.Recoil, math.Rand( -0.5, 0.5 ) *self.Primary.Recoil, 0 ) )
	
	// In singleplayer this function doesn't get called on the client, so we use a networked float
	// to send the last shoot time. In multiplayer this is predicted clientside so we don't need to 
	// send the float.
	if ( (SinglePlayer() && SERVER) || CLIENT ) then
		self.Weapon:SetNetworkedFloat( "LastShootTime", CurTime() )
	end
end

function SWEP:SecondaryAttack()
	self.Weapon:SetNextSecondaryFire( CurTime() + self.Secondary.Delay )
	self.Weapon:SetNextPrimaryFire( CurTime() + self.Secondary.Delay )
	self.NextReload = CurTime() + self.Secondary.Delay

	if self.Weapon:GetNetworkedBool( "Silenced", false ) then
		self.Weapon:SetNetworkedBool( "Silenced", false )
		self.Weapon:SendWeaponAnim(ACT_VM_DETACH_SILENCER)
		self.Primary.Sound = Sound("Weapon_USP.Single")
		self.CSMuzzleFlashes = true
	else
		self.Weapon:SetNetworkedBool( "Silenced", true )
		self.Weapon:SendWeaponAnim(ACT_VM_ATTACH_SILENCER)
		self.Primary.Sound = Sound("Weapon_USP.SilencedShot")
		self.CSMuzzleFlashes = false
	end
end

function SWEP:Think()
	if self.NeedToReload then
		self:Reload()
		return
	end
	
	if self.Weapon:Clip1() < 1 and !self.Owner:KeyDown( IN_ATTACK ) and !self.AutoReloading then
		self:Reload()
		self.AutoReloading = true
		return
	end

	if self.Weapon:Clip1() > 0 then
		self.AutoReloading = false
	end

	if self.Weapon:Clip1() < 1 then
		if self.Weapon:GetNetworkedBool( "Silenced", false ) then
			self.Weapon:SendWeaponAnim(ACT_VM_IDLE_SILENCED)
		else
			self.Weapon:SendWeaponAnim(ACT_VM_IDLE)
		end
	end

end

function SWEP:Reload()
	if self.NextReload > CurTime() then return end


	if self.Owner:KeyDown( IN_ATTACK ) then
		self.NeedToReload = true
		return
	end
	self.NeedToReload = false
	if self.Weapon:GetNetworkedBool( "Silenced", false ) then
		self.Weapon:DefaultReload( ACT_VM_RELOAD_SILENCED )
	else
		self.Weapon:DefaultReload( ACT_VM_RELOAD)
	end
end

function SWEP:Deploy()

	if self.Weapon:GetNetworkedBool( "Silenced", false ) then
		self.Weapon:SendWeaponAnim( ACT_VM_DRAW_SILENCED )
	else
		self.Weapon:SendWeaponAnim( ACT_VM_DRAW )
	end

	self.Weapon:SetNextPrimaryFire( CurTime() + 1 )

	return true
end


function SWEP:CSShootBullet( dmg, recoil, numbul, cone )

	numbul = numbul or 1
	cone = cone or 0.01
	cone = ( self.Owner:GetVelocity():Length() > 150 ) and cone + ( self.Owner:GetVelocity():Length() / 10000 ) or cone

	local bullet = {}
	bullet.Num 		= numbul
	bullet.Src 		= self.Owner:GetShootPos()			// Source
	bullet.Dir 		= self.Owner:GetAimVector()			// Dir of bullet
	bullet.Spread 	= Vector( cone, cone, 0 )			// Aim Cone
	bullet.Tracer	= 2									// Show a tracer on every x bullets 
	bullet.Force	= 5									// Amount of force to give to phys objects
	bullet.Damage	= dmg
	local SWEPOWNER = self.Owner
	bullet.Callback	= function( attacker, tr, dmginfo )
			local dist = ( tr.HitPos - tr.StartPos ):Length()
			if dist > 500 then return end
			local traceres = {}

			local shdist = 40
			shdist = ( tr.MatType == 67 ) and 15 or shdist
			shdist = ( tr.MatType == 77 ) and 5 or shdist

			traceres.start = tr.HitPos + tr.Normal * shdist
			traceres.endpos = tr.HitPos
			local trace = util.TraceLine( traceres )
			if trace.Hit then
				local bullet = {}
				bullet.Num 		= numbul
				bullet.Src 		= trace.HitPos - trace.Normal			// Source
				bullet.Dir 		= tr.Normal	// Dir of bullet
				bullet.Spread 	= Vector( 0, 0, 0 )			// Aim Cone
				bullet.Tracer	= 2									// Show a tracer on every x bullets 
				bullet.Force	= 5									// Amount of force to give to phys objects
				bullet.Damage	= dmg
				SWEPOWNER:LagCompensation( false )
				SWEPOWNER:FireBullets( bullet )
				SWEPOWNER:LagCompensation( true )
			end
		end
	self.Owner:FireBullets( bullet )
	self:TakePrimaryAmmo( 1 )
	if self.Weapon:GetNetworkedBool( "Silenced", false ) then
		self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK_SILENCED )
	else
		self.Weapon:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
		self.Owner:MuzzleFlash()
	end
	self.Owner:MuzzleFlash()
	self.Weapon:EmitSound( self.Primary.Sound )
	self.Owner:SetAnimation( PLAYER_ATTACK1 )								// Crappy muzzle light				// 3rd Person Animation
	if self.Owner:IsNPC() then return end
	
	if ( (SinglePlayer() && SERVER) || ( !SinglePlayer() && CLIENT && IsFirstTimePredicted() ) ) then
	
		local eyeang = self.Owner:EyeAngles()
		eyeang.pitch = eyeang.pitch - recoil
		self.Owner:SetEyeAngles( eyeang )
	end
end